package serialize;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Supplier;

import static java.util.Arrays.asList;

/**
 * Loads locations from file.
 *
 * @author Blind
 */
public class LocationLoader implements Supplier<List<LocationAdapter>> {

    private final String source;

    public LocationLoader(String source) {
        this.source = source;
    }

    public List<LocationAdapter> get() {
        return asList(toYml(ClassLoader.getSystemClassLoader().getResourceAsStream(source)));
    }

    private LocationAdapter[] toYml(InputStream is) {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        try {
            return mapper.readValue(is, LocationAdapter[].class);
        } catch (IOException e) {
            throw new RuntimeException("Exception during parsing", e);
        }
    }
}
